// pages/about/about.js
var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    wifiInfo:''
  },

  mendianId:'',
  guiId:'',

  //要连接的wifi信息
  wifiName:'',
  wifiKey:'',

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.mendianId = options.mendianid ? options.mendianid : '';
    this.guiId = options.guiid ? options.guiid : '';
    //
    this.getWifiInfo();
  },

  //获取配置的wifi信息
  getWifiInfo:function(){
    var that = this;
    util.api({
      data: {
        "publickey": util.publiccode,
        "ac": "getwifiinfo",
        "mendianid": this.mendianId ? this.mendianId : "",
        "guiid": this.guiId ? this.guiId : "",
      },
      success: function (responseData) {
        console.log("后台配置的wifi信息：", responseData);
        if (responseData.data.msg) {
          //报错信息
          wx.showModal({
            title: '提示',
            content: '获取Wi-Fi信息失败，如需要连接请联系服务人员',
            showCancel: false,
            success:function(res){
              wx.navigateBack({
                delta: 1
              })
            }
          })
        } else {
          var wifiInfoArray = responseData.data;
          wifiInfoArray.forEach(function(m, i){
            if (that.guiId == m.bindguiid){//有设置房间wifi信息
              that.wifiName = m.wifiname;
              that.wifiKey = m.wifipass;
            } else if (that.mendianId == m.bindmendianid){//有设置门店wifi
              that.wifiName = m.wifiname;
              that.wifiKey = m.wifipass;
            } else if (util.orgid == m.binduid){//有设置小程序默认wifi 暂不使用
              // that.wifiName = m.wifiname;
              // that.wifiKey = m.wifipass;
            }
          });
          if(that.wifiName && that.wifiKey){
            that.startConnectWiFi();
          } else {
            wx.showModal({
              title: '提示',
              content: '获取Wi-Fi信息失败，如需要连接请联系服务人员',
              showCancel: false,
              success: function (res) {
                wx.navigateBack({
                  delta: 1
                })
              }
            })
          }
        }
      }
    });
  },

  startConnectWiFi:function(){
    var that = this;
    wx.startWifi({
      success(res) {
        console.log("打开Wi-Fi:", res);
        if (res.errCode == 0) {
          //去连接wifi
          if (that.wifiName && that.wifiKey) {
            that.connectWifi(that.wifiName, that.wifiKey);
          } else {
            //测试wifi连接
            that.connectWifi("ZC_MI", "13512733304");
          }
        } else if (res.errCode == 12005) {//Android 特有，未打开 Wi-Fi 开关
          wx.showModal({
            title: '提示',
            content: '手机Wi-Fi未开启，请打开手机Wi-Fi再次扫码尝试连接',
            showCancel: false,
            success: function (res) {
              wx.navigateBack({
                delta: 1
              })
            }
          })
        }
      },
      fail(res) {
        wx.setClipboardData({
          data: that.wifiKey,
        })
        //报错信息
        let toastStr = '初始化Wi-Fi模块失败，请尝试手动连接。wifi名：' + that.wifiName + "   密码：" + that.wifiKey + "   密码已复制到剪切板";
        
        wx.showModal({
          title: '提示',
          content: toastStr,
          showCancel: false,
          success: function (res) {
            wx.navigateBack({
              delta: 1
            })
          }
        })
      }
    })
  },

  connectWifi: function (wifiName, password){
    console.log("要连接的wifi信息：" + wifiName + "; " + password);
    wx.connectWifi({
      SSID: wifiName,
      password: password,
      success: function(res){
        console.log("connectWifi接口调用成功信息:", res);
        if(res.errCode == 0){
          app.dialogToast("Wi-Fi已连接", 'success', function(){
            wx.navigateBack({
              delta: 1
            })
          });
        } else if (res.errCode == 12002 || res.errCode == 12003 || res.errCode == 12008){
          wx.showModal({
            title: '提示',
            content: 'wifi信息有误，请联系服务人员',
            showCancel: false,
            success: function (res) {
              wx.navigateBack({
                delta: 1
              })
            }
          })
        } else {
          wx.showModal({
            title: '提示',
            content: 'Wi-Fi连接失败，请尝试手动连接。错误码：' + res.errCode,
            showCancel: false,
            success: function (res) {
              wx.navigateBack({
                delta: 1
              })
            }
          })
        }
      },
      fail:function(res){
        if (res.errCode == 12002 || res.errCode == 12003 || res.errCode == 12008) {
          wx.showModal({
            title: '提示',
            content: 'wifi信息有误，请联系服务人员',
            showCancel: false,
            success: function (res) {
              wx.navigateBack({
                delta: 1
              })
            }
          })
        } else {
          wx.showModal({
            title: '提示',
            content: 'Wi-Fi连接失败，请尝试手动连接。错误码：' + res.errCode,
            showCancel: false,
            success: function (res) {
              wx.navigateBack({
                delta: 1
              })
            }
          })
        }
      }
    })
  }

})